package top.kindaries.utils;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 执行系统命令
 *
 * @author duxikun
 */
public class Ffmpeg {

    private static final Logger logger = Logger.getLogger(Ffmpeg.class);

    /**
     * 执行命令行并获取进程
     *
     * @param streamUrl    流地址
     * @param m3u8FileName 输出的m3u8文件名
     * @return 返回window处理线程
     * @throws IOException
     */
    public synchronized static Process ffmpeg(String streamUrl, String m3u8FileName) throws IOException {
        String cmdKey;
        if (streamUrl.startsWith("rtsp://")) {
            cmdKey = PropUtil.get("ffmpeg.cmd.rtsp");
        } else {
            cmdKey = PropUtil.get("ffmpeg.cmd.rtmp");
        }
        String appPath = PropUtil.get("ffmpeg.path");
        //从配置文件中读取命令模板，并注入相应的动态参数值
        String cmd = cmdKey.replace("#(ffmpeg_path)", appPath)
                .replace("#(monitor_stream_url)", streamUrl)
                .replace("#(hls_m3u8_path)", m3u8FileName);
        logger.debug("当前执行的命令为：" + cmd);
        //执行命令获取主进程
        return Runtime.getRuntime().exec(cmd);
    }

}
